package cn.mumzone.pos.ui.widgets;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.DragEvent;
import android.view.View;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;

import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.R;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshHeader;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshInternal;
import cn.mumzone.pos.ui.widgets.refresh.api.RefreshLayout;
import cn.mumzone.pos.ui.widgets.refresh.constant.RefreshState;
import cn.mumzone.pos.ui.widgets.refresh.internal.InternalAbstract;

/**
 * Created by JoeLjt on 2019/7/18.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class PosRefreshHeader extends InternalAbstract implements RefreshHeader {

    private TextView mRefreshText;
    private LottieAnimationView mRefreshView;

    public PosRefreshHeader(Context context) {
        this(context, null);
    }

    public PosRefreshHeader(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PosRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        View.inflate(context, R.layout.layout_pos_refresh_header, this);

        mRefreshText = findViewById(R.id.tv_refresh_state);
        mRefreshView = findViewById(R.id.animation_view);
//        mRefreshView.setImageAssetsFolder("images/");

    }

    @Override
    public int onFinish(@NonNull RefreshLayout layout, boolean success) {

        if (success) {
            mRefreshText.setText("刷新完成");
        } else {
            mRefreshText.setText("刷新失败");
        }
        return 300;
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
        final TextView refreshText = mRefreshText;
        final LottieAnimationView refreshView = mRefreshView;
        switch (newState) {
            case PullDownToRefresh:
                refreshText.setText("下拉刷新");
                break;
            case Refreshing:
            case RefreshReleased:
                refreshText.setText("正在刷新...");
                break;
            case ReleaseToRefresh:
                refreshText.setText("释放以刷新");
                break;
        }
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
        super.onMoving(isDragging, percent, offset, height, maxDragHeight);

        final LottieAnimationView refreshView = mRefreshView;

        if (offset <= height) {
            refreshView.setProgress((offset - 10) * 1.0f / height+10);
        }

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
        super.onReleased(refreshLayout, height, maxDragHeight);
        mRefreshView.playAnimation();
    }



}
